﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using System.Text;
namespace Web
{
    public partial class AdminExamArea : System.Web.UI.Page
    {
        protected int examroomid = 0;
        public int canShow = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterExamroom"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (!PageValidate.QueryStringIsEmpty("examroomid"))
            {
                try
                {
                    examroomid = int.Parse(PageValidate.QueryString("examroomid"));
                }
                catch
                {
                    Response.Redirect("AdminExamRoom.aspx");
                    return;
                }
            }
            else
            {
                Response.Redirect("AdminExamRoom.aspx");
                return;
            }


            UserLinkExtendInfo uinfo = new UserLinkExtendInfo();
            if (this.Request.Cookies["xxadmin"] != null)
            {
                if (this.Request.Cookies["xxadmin"]["UserId"] != "")
                {
                    uinfo = UserLinkExtend.GetSingleInfo(int.Parse(this.Request.Cookies["xxadmin"]["UserId"]));
                }
            }
            if (this.Request.Cookies["Teacherxxx"] != null)
            {
                uinfo = UserLinkExtend.GetSingleInfo(int.Parse(this.Request.Cookies["Teacherxxx"]["UserId"]));
            }
            UserInfo User_Info = Users.GetUserInfoById(uinfo.Id);
            int XueQu = User_Info.XueQu;
            int RoleId = User_Info.RoleId;
            IfCanShow(RoleId);

            if (!IsPostBack)
            {
                ExamRoomInfo examroomInfo = ExamRoom.GetSingleInfo(examroomid);
                if (examroomInfo != null)
                {
                    string[] arrAreaIds = PageValidate.IsEmpty(examroomInfo.ExamAreaId) ? null : examroomInfo.ExamAreaId.Split(',');
                    IList<int> ids = new List<int>();
                    if (XueQu == 0)
                    {
                        for (int i = 0; i < arrAreaIds.Length; i++)
                        {
                            ids.Add(int.Parse(arrAreaIds[i]));
                        }
                    }
                    else
                    {
                        if (arrAreaIds.Contains(XueQu + ""))
                            ids.Add(XueQu);
                    }
                    BindExamArea(ids);
                }
                else
                {
                    Response.Redirect("AdminExamRoom.aspx");
                }
            }
        }

        private void IfCanShow(int roleid)
        {
            if (roleid == 1 || roleid == 2 || roleid == 7 || roleid == 51 || roleid == 52)
                canShow = 1;
            else
                canShow = 0;
        }

        private void BindExamArea(IList<int> ids)
        {
            IList<AreaInfo> areaInfos = Area.GetList(ids);
            this.ExamAreaList.DataSource = areaInfos;
            this.ExamAreaList.DataBind();
        }

        protected int GetStudentCount(object areaid)
        {
            return StudentScoreExtend.GetCount(examroomid, Convert.ToInt32(areaid), 0, 0, 0, 0);
        }

        protected decimal GetAverage(object areaid)
        {
            int num = StudentScoreExtend.GetCount(examroomid, Convert.ToInt32(areaid), 0, 0, 0, 0);
            decimal score = StudentScoreExtend.GetScoreCount(examroomid, Convert.ToInt32(areaid), 0, 0, 0, 0);
            if (score > 0)
            {
                return Math.Round(score / num, 1);
            }
            else
            {
                return 0;
            }
        }
    }
}
